import 'dart:convert';
import 'dart:ui';

import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:logger/logger.dart';
import 'package:modal_bottom_sheet/modal_bottom_sheet.dart';
import 'package:xim/business/model/message.dart';
import 'package:xim/business/model/secret.dart';
import 'package:xim/business/model/session.dart';
import 'package:xim/business/model/user.dart';
import 'package:xim/common/util/collection.dart';
import 'package:xim/common/util/device.dart';
import 'package:xim/config/store_config.dart';
import 'package:xim/http/rsa.dart';
import 'package:xim/http/store.dart';
import 'package:fast_rsa/fast_rsa.dart';
import './http.dart';
import 'package:web_socket_channel/web_socket_channel.dart';
var logger = Logger();

/// ----------------- WS-----------------------

class WsRequest {
  late final WebSocketChannel channel;

  void Function(dynamic event) listen;
  void Function(dynamic error)? onError;
  void Function()? onDone;

  WsRequest._(Uri uri, {String? token, required this.listen,this.onError, this.onDone,}){
    channel= WebSocketChannel.connect(uri);
    channel.ready.then((value) {
      logger.i("ws connection success");
      // 认证
      // sendMessage(AuthMessage(token??''));
      // 绑定监听
      channel.stream.listen(listen, onError: onError, onDone: onDone);
    });
  }

  static WsRequest? _instance;

  /// 发送消息
  // void sendMessage(Message message){
  //   channel.sink.add(message.toJsonStr());
  // }

  static Future<WsRequest> instance({String? token}) async {
    if(_instance !=null){
      return Future.value(_instance);
    }
    var uri = Uri.parse('ws://$baseIp:9000/ws');
    token = token ?? await StoreApi.instance.takeUserToken();
    _instance = WsRequest._(uri, token: token, listen: (data){
      logger.i("receive msg $data");
    });
    return Future.value(_instance);
  }
}

void toast(String msg, {BuildContext? context}){
  var brightness = context!=null?PlatformUtils.getBrightness(context):Brightness.light;
  Fluttertoast.showToast(
      msg: msg,
      toastLength: Toast.LENGTH_SHORT,
      gravity: ToastGravity.BOTTOM,
      timeInSecForIosWeb: 1,
      backgroundColor: brightness==Brightness.light?const Color(0xfff0f0f0):const Color(0xff4f4f4f),
      textColor: brightness==Brightness.light?const Color(0xff4f4f4f):const Color(0xfff0f0f0),
      fontSize: 14.0
  );
}

/// 对话框
void showLoginBottomSheet(BuildContext context){
  showMaterialModalBottomSheet(
    context: context,
    backgroundColor: Colors.transparent,
    builder: (context) => Container(
      decoration: const BoxDecoration(
          color: Colors.red,
          borderRadius: BorderRadius.only(
              topLeft: Radius.circular(8),
              topRight: Radius.circular(8)
          )
      ),
      height: 0.4.sh,
      child: Column(
        children: [

        ],
      ),
    ),
  );
}


void bottomSheet(BuildContext context, Widget content, {Color bgColor=Colors.white, bool overlay=true, EdgeInsets? padding}){
  showMaterialModalBottomSheet(
    context: context,
    backgroundColor: Colors.transparent,
    builder: (context) => Container(
      padding: padding??EdgeInsets.zero,
      decoration: BoxDecoration(
          color: bgColor,
          borderRadius: const BorderRadius.only(
              topLeft: Radius.circular(8),
              topRight: Radius.circular(8)
          )
      ),
      height: 0.4.sh,
      child: content,
    ),
  );
}

